﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Text;
using System.Xml;

namespace MemoGame
{
    class HallOfFame
    {
        Dictionary<int, List<HOFItem>> ranking = new Dictionary<int, List<HOFItem>>();

        internal Dictionary<int, List<HOFItem>> Ranking
        {
            get { return ranking; }
            set { ranking = value; }
        }        

        public void fillRanking()
        {
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load("../../Resources/hall_of_fame.xml");
                XmlNodeList items = doc.SelectSingleNode("hof").SelectNodes("items");
                Console.WriteLine("hof");
                Console.WriteLine("hof "+items.Count);
                int i = 0;
                foreach (XmlNode item in items)
                {
                    Console.WriteLine("NULL: " + (item.Attributes["size"] == null));

                    if (item.Attributes["size"] != null)
                    {
                        List<HOFItem> hofItems = new List<HOFItem>();
                        foreach(XmlNode n in item.ChildNodes)
                        {
                            hofItems.Add(new HOFItem(n.SelectSingleNode("player").InnerText,
                                n.SelectSingleNode("time").InnerText));
                        }
                        ranking.Add(int.Parse(item.Attributes["size"].Value), hofItems);
                    }
                    i++;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: "+e.Message);
            }
        }

        public void saveRanking()
        {
            XmlWriterSettings ws = new XmlWriterSettings();
            ws.Indent = true;
            XmlWriter writer = XmlWriter.Create("../../Resources/hall_of_fame.xml", ws);
            writer.WriteStartDocument();
            writer.WriteStartElement("hof");
            Console.WriteLine("items "+ranking.Count);
            for (int i = 0; i < 4; i++)
            {
                int ii = (i + 1) * 2;
                writer.WriteStartElement("items");
                writer.WriteAttributeString("size", ii.ToString());
                if (ranking.ContainsKey(ii))
                {
                    foreach (HOFItem item in ranking[ii])
                    {
                        if (item.Player.Equals("") || item.Time.Equals(""))
                        {
                            continue;
                        }
                        Console.WriteLine("Item: " + item.Player + " - " + item.Time);
                        writer.WriteStartElement("item");
                        writer.WriteElementString("player", item.Player);
                        writer.WriteElementString("time", item.Time);
                        writer.WriteEndElement();
                    }
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }

        public void addItem(int size, String login, String time)
        {
            Boolean shouldLook = true;
            for (int i = 0; i < ranking.Count; i++)
            {
                if (ranking.ElementAt(i).Key == size)
                {
                    foreach (HOFItem item in ranking.ElementAt(i).Value)
                    {
                        Console.WriteLine("addItem " + item.Player.Equals("") + " - " + item.Time.Equals(""));
                        if (item.Player.Equals("") || item.Time.Equals(""))
                        {
                            item.Player = login;
                            item.Time = time;
                            shouldLook = false;
                            break;
                        }
                    }
                }
            }

            if (shouldLook)
            {
                HOFItem newItem = new HOFItem(login, time);
                for (int i = 0; i < ranking.Count; i++)
                {
                    if (ranking.ElementAt(i).Key == size)
                    {
                        foreach (HOFItem item in ranking.ElementAt(i).Value)
                        {
                            Console.WriteLine("addItem2 " + item.Player.Equals("") + " - " + item.Time.Equals(""));
                            if (item.CompareTo(newItem) < 0)
                            {
                                item.Player = login;
                                item.Time = time;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
